package com.hang.ad.service;

import com.alibaba.fastjson.JSON;
import com.hang.ad.Application;
import com.hang.ad.constant.CommonStatus;
import com.hang.ad.dao.AdCreativeDao;
import com.hang.ad.dao.AdPlanDao;
import com.hang.ad.dao.AdUnitDao;
import com.hang.ad.dao.unit_condition.AdUnitDistrictDao;
import com.hang.ad.dao.unit_condition.AdUnitItDao;
import com.hang.ad.dao.unit_condition.AdUnitKeywordDao;
import com.hang.ad.dao.unit_condition.CreativeUnitDao;
import com.hang.ad.dump.DataConstant;
import com.hang.ad.dump.table.*;
import com.hang.ad.entity.AdCreative;
import com.hang.ad.entity.AdPlan;
import com.hang.ad.entity.AdUnit;
import com.hang.ad.entity.unit_condition.AdUnitDistrict;
import com.hang.ad.entity.unit_condition.AdUnitIt;
import com.hang.ad.entity.unit_condition.AdUnitKeyword;
import com.hang.ad.entity.unit_condition.CreativeUnit;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.CollectionUtils;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class},webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class DumpDataService {
    @Autowired
    private AdPlanDao adPlanDao;
    @Autowired
    private AdUnitDao adUnitDao;
    @Autowired
    private AdCreativeDao adCreativeDao;
    @Autowired
    private CreativeUnitDao creativeUnitDao;
    @Autowired
    private AdUnitDistrictDao adUnitDistrictDao;
    @Autowired
    private AdUnitItDao adUnitItDao;
    @Autowired
    private AdUnitKeywordDao adUnitKeywordDao;

    @Test
    public void dumpData(){
        dumpAdPlan( String.format("%s%s", DataConstant.DATA_ROOT_DIR,DataConstant.AD_PLAN));
        dumpAdUnit(String.format("%s%s", DataConstant.DATA_ROOT_DIR,DataConstant.AD_UNIT));
        dumpAdCreative(String.format("%s%s", DataConstant.DATA_ROOT_DIR,DataConstant.AD_CREATIVE));
        dumpCreativeUnit(String.format("%s%s", DataConstant.DATA_ROOT_DIR,DataConstant.AD_CREATIVE_UNIT));
        dumpUnitDistrict(String.format("%s%s", DataConstant.DATA_ROOT_DIR,DataConstant.AD_UNIT_DISTRICT));
        dumpUnitIt(String.format("%s%s", DataConstant.DATA_ROOT_DIR,DataConstant.AD_UNIT_IT));
        dumpUnitKeyword(String.format("%s%s", DataConstant.DATA_ROOT_DIR,DataConstant.AD_UNIT_KEYWORD));
    }

    private void dumpAdPlan(String fileName){
        List<AdPlan> adPlans = adPlanDao.findAllByPlanStatus(CommonStatus.VALID.getStatus());
        if (CollectionUtils.isEmpty(adPlans)){
            return;
        }
        List<AdPlanTable> adPlanTables = new ArrayList<>();
        adPlans.forEach(plan -> adPlanTables.add(
                new AdPlanTable(plan.getId(),
                                plan.getUserId(),
                                plan.getPlanStatus(),
                                plan.getStartDate(),
                                plan.getEndDate())));
        Path path = Paths.get(fileName);
        try (BufferedWriter writer = Files.newBufferedWriter(path)){
            for (AdPlanTable table : adPlanTables){
                writer.write(JSON.toJSONString(table));
                writer.newLine();
            }
            writer.close();
        } catch (IOException e) {
            log.error("dumpAdPlan error");
        }
    }

    private void dumpAdUnit(String fileName){
        List<AdUnit> adUnits = adUnitDao.findAllByUnitStatus(CommonStatus.VALID.getStatus());
        if (CollectionUtils.isEmpty(adUnits)){
            return;
        }
        List<AdUnitTable> adUnitTables = new ArrayList<>();

        adUnits.forEach(unit -> adUnitTables.add(
                new AdUnitTable(unit.getId(),
                        unit.getUnitStatus(),
                        unit.getPositionType(),
                        unit.getPlanId())
        ));
        Path path = Paths.get(fileName);
        try (BufferedWriter writer = Files.newBufferedWriter(path)){
            for (AdUnitTable table : adUnitTables){
                writer.write(JSON.toJSONString(table));
                writer.newLine();
            }
            writer.close();
        } catch (IOException e) {
            log.error("dumpAdUnit error");
        }
    }

    private void dumpAdCreative(String fileName){
        List<AdCreative> adCreatives = adCreativeDao.findAll();
        if (CollectionUtils.isEmpty(adCreatives)){
            return;
        }
        List<AdCreativeTable> adCreativeTables = new ArrayList<>();

        adCreatives.forEach(creative-> adCreativeTables.add(
                new AdCreativeTable(creative.getId(),
                                    creative.getName(),
                                    creative.getType(),
                                    creative.getMaterialType(),
                                    creative.getHeight(),
                                    creative.getWidth(),
                                    creative.getAuditStatus(),
                                    creative.getUrl())
        ));
        Path path = Paths.get(fileName);
        try (BufferedWriter writer = Files.newBufferedWriter(path)){
            for (AdCreativeTable table : adCreativeTables){
                writer.write(JSON.toJSONString(table));
                writer.newLine();
            }
            writer.close();
        } catch (IOException e) {
            log.error("dumpAdCreative error");
        }
    }

    private void dumpCreativeUnit(String fileName){
        List<CreativeUnit> creativeUnits = creativeUnitDao.findAll();
        if (CollectionUtils.isEmpty(creativeUnits)){
            return;
        }
        List<AdCreativeUnitTable> adCreativeUnitTables = new ArrayList<>();
        creativeUnits.forEach(c-> adCreativeUnitTables.add(
                new AdCreativeUnitTable(c.getCreativeId(),
                                        c.getUnitId())
        ));
        Path path = Paths.get(fileName);
        try (BufferedWriter writer = Files.newBufferedWriter(path)){
            for (AdCreativeUnitTable table : adCreativeUnitTables){
                writer.write(JSON.toJSONString(table));
                writer.newLine();
            }
            writer.close();
        } catch (IOException e) {
            log.error("dumpAdCreativeUnit error");
        }
    }

    private void dumpUnitDistrict(String fileName){
        List<AdUnitDistrict> districts = adUnitDistrictDao.findAll();
        if (CollectionUtils.isEmpty(districts)){
            return;
        }
        List<AdUnitDistrictTable> adUnitDistrictTables = new ArrayList<>();
        districts.forEach(d -> adUnitDistrictTables.add(
                new AdUnitDistrictTable(d.getUnitId(),
                                        d.getProvince(),
                                        d.getCity())
        ));
        Path path = Paths.get(fileName);
        try (BufferedWriter writer = Files.newBufferedWriter(path)){
            for (AdUnitDistrictTable table : adUnitDistrictTables){
                writer.write(JSON.toJSONString(table));
                writer.newLine();
            }
            writer.close();
        } catch (IOException e) {
            log.error("dumpUnitDistrict error");
        }
    }

    private void dumpUnitIt(String fileName){
        List<AdUnitIt> adUnitIts = adUnitItDao.findAll();
        if (CollectionUtils.isEmpty(adUnitIts)){
            return;
        }
        List<AdUnitItTable> adUnitItTables = new ArrayList<>();
        adUnitIts.forEach(a-> adUnitItTables.add(
                new AdUnitItTable(a.getUnitId(),
                                  a.getItTag())
        ));
        Path path = Paths.get(fileName);
        try (BufferedWriter writer = Files.newBufferedWriter(path)){
            for (AdUnitItTable table : adUnitItTables){
                writer.write(JSON.toJSONString(table));
                writer.newLine();
            }
            writer.close();
        } catch (IOException e) {
            log.error("dumpUnitIt error");
        }
    }

    private void dumpUnitKeyword(String fileName){
        List<AdUnitKeyword> keywords = adUnitKeywordDao.findAll();
        if (CollectionUtils.isEmpty(keywords)){
            return;
        }
        List<AdUnitKeywordTable> adUnitKeywordTables = new ArrayList<>();
        keywords.forEach(k-> adUnitKeywordTables.add(
                new AdUnitKeywordTable(k.getUnitId(),
                                       k.getKeyword())
        ));
        Path path = Paths.get(fileName);
        try (BufferedWriter writer = Files.newBufferedWriter(path)){
            for (AdUnitKeywordTable table : adUnitKeywordTables){
                writer.write(JSON.toJSONString(table));
                writer.newLine();
            }
            writer.close();
        } catch (IOException e) {
            log.error("dumpUnitKeyword error");
        }
    }
}
